package com.tony.study.线程试题;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: ListAddRemoveByThread
 * @Auther: 皮蛋布丁
 * @Date: 2021/11/25/14:48
 * @Description: 创建4个线程，其中其中2个往集合中添加数据，另外2个删除数据
 */
public class ListAddRemoveByThread {
    public static void main(String[] args) {
        CopyOnWriteArrayListDemo listDemo = new CopyOnWriteArrayListDemo();
        new Thread(()->{listDemo.printA();},"A").start();
        new Thread(()->{listDemo.printB();},"B").start();
        new Thread(()->{listDemo.printC();},"C").start();
        new Thread(()->{listDemo.printD();},"D").start();
    }
}
class CopyOnWriteArrayListDemo{
    List<String> list = new CopyOnWriteArrayList<>();
    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();
    private Condition condition4 = lock.newCondition();
    private int number = 1;

    public void printA(){
        lock.lock();
        try {
            while (number!=1) {
                condition1.await();
            }
            list.add(UUID.randomUUID().toString().substring(0,5));
            System.out.println(Thread.currentThread().getName()+"=> "+list);
            number = 2;
            condition2.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    public void printB(){
        lock.lock();
        try {
            while (number!=2) {
                condition2.await();
            }
            list.add(UUID.randomUUID().toString().substring(0,5));
            System.out.println(Thread.currentThread().getName()+"=> "+list);
            number = 3;
            condition3.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    public void printC(){
        lock.lock();
        try {
            while (number!=3) {
                condition1.await();
            }
            list.remove(0);
            System.out.println(Thread.currentThread().getName()+"=> "+list);
            number = 4;
            condition4.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    public void printD(){
        lock.lock();
        try {
            while (number!=4) {
                condition4.await();
            }
            list.remove(0);
            System.out.println(Thread.currentThread().getName()+"=> "+list);
            number = 1;
            condition1.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
